/*
 * PopupMenuDialog.java
 *
 * Created on 2004/11/26 +0900
 *
 * $Revision: 59 $
 * $Date: 2008-12-28 19:22:21 +0000 (Sun, 28 Dec 2008) $
 */

package org.gikolet.framework.ui;

import org.gikolet.framework.ui.events.ActionEvent;
import org.gikolet.framework.ui.events.ActionListener;
import org.gikolet.framework.ui.events.KeyEvent;
import org.gikolet.framework.ui.events.KeyEventListener;

/**
 * @author Tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 59 $ $Date: 2008-12-28 19:22:21 +0000 (Sun, 28 Dec 2008) $
 */
class PopupMenuDialog extends Dialog {
    private Menu menu;
    private RootControl _root;
    private List list;

    private PopupMenuDialog menuDialog;
    private PopupMenuDialog parent;

    private MenuItem _cancelMenuItem;

    PopupMenuDialog(RootControl root, Menu menu) {
	super(root, null, true);

	this._root = root;

	this.list = new List();
	this.menu = menu;
	this._cancelMenuItem = new MenuItem("取消");

	setMenuItem(this._cancelMenuItem);
	this._cancelMenuItem.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		setVisible(false);
	    }
	});

	this.list.addKeyEventListener(new KeyEventListener() {
	    public void keyAction(KeyEvent e) {
		if (e.getKeyActionType() == KeyEvent.PRESSED
			&& (e.getKeyCode() == KeyEvent.ENTER || e.getKeyCode() == KeyEvent.KEY_NUM5)) {
		    clicked();
		}
	    }
	});

	updateUI();

	// setLayoutManager(null);

	add(this.list);
    }

    public PopupMenuDialog getPopupMenuDialogParent() {
	return this.parent;
    }

    // PopupMenuDialogにキャンセルを実装したほうが良いけどね
    private void clicked() {
	int index = this.list.getSelectedIndex();
	MenuItem menuItem = this.menu.get(index);
	menuItem.fireAction();
	if (menuItem instanceof Menu) {
	    Menu menu = (Menu) menuItem;
	    if (menu.getMenuItemSize() > 0) {
		this.menuDialog = new PopupMenuDialog(this._root, menu);
		this.menuDialog.parent = this;

		this.menuDialog.setVisible(true);
		return;
	    }
	}
	PopupMenuDialog parent = this;
	do {
	    parent.setVisible(false);
	    parent = parent.parent;
	} while (parent != null);
    }

    void updateUI() {
	if (this.menuDialog != null) {
	    this.menuDialog.updateUI();
	}
	if (this.menu.getMenuItemSize() == 0) {
	    setVisible(false);
	}
	this.list.clear();
	for (int i = 0; i < this.menu.getMenuItemSize(); i++) {
	    MenuItem mi = this.menu.get(i);
	    String str = mi.getLabel();
	    /*
	     * if (mi instanceof Menu) { Menu m = (Menu) mi; }
	     */
	    this.list.add(str);
	}
    }

    protected void layout() {
	this.list.setBounds(getClientArea());
    }

    public Dimension getPreferredSize(int hintWidth, int hintHeight) {
	Dimension pd = this.list.getPreferredSize(hintWidth, hintHeight);
	pd.setWidth(getLeftInset() + pd.getWidth() + getRightInset());
	pd.setHeight(getTopInset() + pd.getHeight() + getBottomInset());

	return pd;
    }

    public PopupMenuDialog getEndPopupMenuDialog() {
	if (this.menuDialog != null) {
	    return this.menuDialog;
	}
	return this;
    }

    public void setVisible(boolean visible) {
	if (visible == isVisible()) {
	    return;
	}
	if (visible) {
	    /*
	     * if (this.menu.getMenuItemSize() == 0) { this.menu.fireAction();
	     * return; }
	     */
	    Display display = getDisplay();
	    if (display != null) {
		int sWidth = display.getScreenWidth();
		int sHeight = display.getScreenHeight();
		Dimension d = getPreferredSize(DEFAULT_LAYOUT, DEFAULT_LAYOUT);

		int hintWidth = DEFAULT_LAYOUT;
		int hintHeight = DEFAULT_LAYOUT;
		if (d.getWidth() > sWidth) {
		    hintWidth = sWidth;
		}
		if (d.getHeight() > sHeight) {
		    hintHeight = sHeight;
		}
		if (hintWidth != DEFAULT_LAYOUT || hintHeight != DEFAULT_LAYOUT) {
		    d = getPreferredSize(hintWidth, hintHeight);
		}

		d.setSize(Math.min(d.getWidth(), display.getScreenWidth()),
			Math.min(d.getHeight(), display.getScreenHeight()));

		int x = (display.getScreenWidth() - d.getWidth()) / 2;
		int y = (display.getScreenHeight() - d.getHeight()) / 2;

		setBounds(x, y, d.getWidth(), d.getHeight());

		// this.menu.addMenuListener(this.menuListener);
		this.menu._popup = this;
	    }
	} else {
	    if (this.menuDialog != null) {
		this.menuDialog.setVisible(false);
	    }
	    if (this.parent != null) {
		this.parent.menuDialog = null;
	    }
	    this.menu._popup = null;
	    // this.menu.removeMenuListener(this.menuListener);
	}
	super.setVisible(visible);

    }
    /*
     * private MenuListener menuListener = new MenuListener() { public void
     * intervalAdded(Menu source, MenuItem child, int index) { updateUI(); }
     * public void intervalRemoved(Menu source, MenuItem child, int index) {
     * updateUI(); } };
     */
}
